<?php
namespace app\admin\controller\v1;
use app\admin\Adminbase;
use app\common\model\Shop as ShopModel;

class Shop extends Adminbase 
{
	
	/**
     * 获取店铺分类
     * /admin/v1/Shop/xmlCate
     * 需要登录
     * @is_need_login true
     * @method post
     */
	public function xmlCate($shop_id=''){
		$request =  request();
		$shop_id = $shop_id ?: $request->userInfo['shop_id'];
		try {
			$model = model('ShopCate');
			$model = $model->where("is_del",0);
			$model = $model->field("id,pid,name as label,id as value");
			$model = $model->order("sort_no");
			$cates = $model->select()->toArray();
			array_unshift($cates,["label"=>"选择店铺分类","value"=>""]);    
			return $request->isAjax() ? $this->ReSucess($cates) : $cates;	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
     * 获取店铺分类
     * /admin/v1/Shop/xmlShopsCate
     * 需要登录
     * @is_need_login true
     * @method post
     */
    public function xmlShopsCate($type=''){
    	$shop_id = request()->userInfo['shop_id'];
        try {
            $model = new ShopModel();
            $model = $model-> where("is_del",0);
            $model = $model-> where("status",1);
            $model = $model-> where("is_enable",1);
            if($type==1){
            	$model = $model-> where("id",'<>',0);
            }
            if($shop_id) $model = $model->where("id",$shop_id);
            $model = $model-> field("name as label,id as value");
            $cates = $model-> select()->toArray();
            if(!$shop_id && $type!=2) array_unshift($cates,["label"=>"--所属门店--","value"=>""]);    
            return $cates;
        } catch (\Exception $e) {
            return $this->ReError($e->getMessage());
        }
    }
	
	/**
	* 商家审核
	* /admin/v1/shop/approval
	* 需要登录 
	* @is_need_login true
	*/
	public function approval($id){
		try {
			model('ShopApply')->approval($id);
			return $this->ReSucess('','审核成功！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 驳回申请
	* /admin/v1/shop/reject
	*/
	public function reject(String $ids,String $desc){
		try {
			model('ShopApply')->reject($ids,$desc);
			return $this->ReSucess('','已驳回！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 添加商家
	* /admin/v1/shop/add
	* 需要登录 
	* @is_need_login true
	*/
	public function add(){
		$request = request();
		$userId = $request ->userId;
		$param = request()->param();
		
		try {
			$dbShop = model('Shop');
			$result = $dbShop->addShop($param);
			if(!$result){
				return $this->ReError(  $dbShop->errorInfo );
			}
			return $this->ReSucess('','申请成功，等待审核！');	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 效验保存
	* /admin/v1/Store/saveFrontValidity
	* 需要登录 
	* @is_need_login true
	*/
	public function saveFrontValidity(){
		$phone =  request()->param('phone','');
		try {
			$result = model('SpreadUser') ->where("phone",$phone) ->find();
			return $this->ReSucess( !$result ? 0 : $result);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 提现驳回
	* /admin/v1/Shop/withdrawReject
	* 需要登录 
	* @is_need_login true
	*/
	public function withdrawReject($id,$desc){
		if(empty($id) || empty($desc)){
			return $this->ReError("参数错误！");
		}
		try {
			$result = model('ShopExtract')->reject($id,$desc);
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	
	/**
	* 提现确认
	* /admin/v1/Shop/withdrawConfirm
	* 需要登录 
	* @is_need_login true
	*/
	public function withdrawConfirm($id){
		if(empty($id)){
			return $this->ReError("参数错误！");
		}
		try {
			$result = model('ShopExtract')->confirm($id);
			return $this->ReSucess(true);	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 获取预约时间
	* /admin/v1/shop/getYyTime
	* 需要登录 
	* @is_need_login true
	*/
	public function getYyTime($shop_id=''){
		try {
			return $this->ReSucess( model('shop')->where('id',$shop_id)->value('time') );	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
	/**
	* 保存预约时间
	* /admin/v1/shop/saveYyTime
	* 需要登录 
	* @is_need_login true
	*/
	public function saveYyTime($shop_id,$time=''){
		try {
			if( !$shop = model('shop')->get($shop_id) ){
				return $this->ReError("店铺不存在！");
			}
			$shop->time = $time;
			$shop->save();
			return $this->ReSucess();	
		} catch (\Exception $e) {
			return $this->ReError($e->getMessage());	
		}
	}
	
}



